package com.fhz.day010;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private WebView webView;
    private ProgressBar bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //在自己的webView中打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                bar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                bar.setVisibility(View.GONE);
            }
        });

        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                bar.setProgress(newProgress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                result.cancel();
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

                return true;

            }
        });

        WebSettings settings = webView.getSettings();
        //设置js
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);


        //添加接口
        webView.addJavascriptInterface(this,"feng");

    }

    public void send(View view) {
//        String url = "http://www.baidu.com";
//        Toast.makeText(this, "adf", Toast.LENGTH_SHORT).show();
//        webView.loadUrl(url);

        webView.loadUrl("file:///android_asset/hello.html");

    }

    @JavascriptInterface
    public void show(){
        Toast.makeText(this, "show", Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void show1(String s){
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn);
        webView = (WebView) findViewById(R.id.web_view);
        bar = (ProgressBar) findViewById(R.id.bar);
    }

    public void back(View view) {
        if(webView.canGoBack()){
            webView.goBack();
        }
    }

    public void forword(View view) {
        if(webView.canGoForward()){
            webView.goForward();
        }
    }

    public void calljs(View view) {
//        webView.loadUrl("javascript:call()");
        webView.evaluateJavascript("javascript:callC()", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
            }
        });
//        webView.loadUrl("javascript:callC()");
    }

    public void calljscan(View view) {
        String s = "hahah";
        webView.loadUrl("javascript:call1('"+s+"')");
    }
}